package com.android.pdd.baseapp.hook;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;


public class CommonTipDialog extends BaseDialog implements View.OnClickListener {
    TextView tvDialogTip;
    TextView tvTitle;
    TextView btnDialogCancle;
    TextView btnDialogOk;

    public CommonTipDialog(Context context) {
        super(context);
    }

    @Override
    protected View provideView() {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setBackgroundColor(0xffffffff);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        linearLayout.setPadding(dp2px(20), dp2px(20), dp2px(20), dp2px(20));
        linearLayout.setLayoutParams(params);

        tvDialogTip = new TextView(context);
        tvDialogTip.setText("提示");
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tvDialogTip.setTextColor(0xff000000);
        tvDialogTip.setTextSize(17);
        tvDialogTip.setLayoutParams(layoutParams);
        linearLayout.addView(tvDialogTip);

        tvTitle = new TextView(context);
        tvTitle.setText("提示信息");
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tvTitle.setTextColor(0xff000000);
        tvTitle.setTextSize(16);
        tvTitle.setLayoutParams(layoutParams);
        linearLayout.addView(tvTitle);

        LinearLayout linearLayout1 = new LinearLayout(context);
        linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = dp2px(20);
        layoutParams.gravity = Gravity.RIGHT;
        linearLayout1.setLayoutParams(layoutParams);

        btnDialogCancle = new TextView(context);
        btnDialogCancle.setText("取消");
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        btnDialogCancle.setTextSize(16);
        btnDialogCancle.setLayoutParams(layoutParams);
        btnDialogCancle.setVisibility(View.GONE);
        btnDialogCancle.setPadding(dp2px(10), dp2px(7), dp2px(10), dp2px(7));
        linearLayout1.addView(btnDialogCancle);


        btnDialogOk = new TextView(context);
        btnDialogOk.setText("确定");
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        btnDialogOk.setTextSize(16);
        btnDialogOk.setTextColor(0xff008B8B);
        btnDialogOk.setLayoutParams(layoutParams);
        btnDialogOk.setVisibility(View.GONE);
        btnDialogOk.setPadding(dp2px(10), dp2px(7), dp2px(10), dp2px(7));
        linearLayout1.addView(btnDialogOk);

        linearLayout.addView(linearLayout1);
        return linearLayout;
    }

    @Override
    protected void initView() {
        btnDialogCancle.setOnClickListener(this);
        btnDialogOk.setOnClickListener(this);

        setWidth(getScreenWidth(context) * 24 / 30);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void initData() {

    }


    public static CommonTipDialog newInstance(Activity activity) {
        return new CommonTipDialog(activity);
    }


    public CommonTipDialog tip(CharSequence charSequence) {
        tvDialogTip.setText(charSequence);
        return this;
    }

    public CommonTipDialog title(CharSequence charSequence) {
        tvTitle.setText(charSequence);
        return this;
    }

    public CommonTipDialog setCancleButton(OnClickListener clickListener) {
        return setCancleButton(null, clickListener);
    }

    public CommonTipDialog setCancleButton(CharSequence text) {
        return setCancleButton(text, null);
    }

    public CommonTipDialog setCancleButton(CharSequence text, final OnClickListener clickListener) {
        btnDialogCancle.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text)) {
            btnDialogCancle.setText(text);
        }
        if (clickListener != null) {
            btnDialogCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.onClick(CommonTipDialog.this, 0);
                }
            });
        } else if (TextUtils.isEmpty(text)) {
            btnDialogCancle.setVisibility(View.GONE);
        }
        return this;
    }

    public CommonTipDialog setOkButton(OnClickListener clickListener) {
        return setOkButton(null, clickListener);
    }

    public CommonTipDialog setOkButton(CharSequence text) {
        return setOkButton(text, null);
    }


    public CommonTipDialog setOkButton(CharSequence text, final OnClickListener clickListener) {
        btnDialogOk.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text)) {
            btnDialogOk.setText(text);
        }
        if (clickListener != null) {
            btnDialogOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.onClick(CommonTipDialog.this, 0);
                }
            });
        }
        return this;
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }
}
